
-- EliteMatchBookView
local CrossServerEliteMatchHelper = import(".CrossServerEliteMatchHelper")
local UIUtil = require("app.utils.UIUtil")
local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local EliteMatchBookView = class(fileName, app.mvc.ViewBase)

EliteMatchBookView.VIEW_ROOT_RES = Res.CSEliteMatchBookScene

function EliteMatchBookView:init()
    self._ui.Button_close:addClickEventListener(function()
		  executeFunc(self._onCloseView)
    end)
    
    -- left
    self._tltText = self._ui["Panel/Panel_left/tlt"]
    self._descText = self._ui["Panel/Panel_left/Text"]
    self._icon = self._ui["Panel/Panel_left/Image"]
    self._leftBtn = self._ui["Panel/Panel_left/Button"]

    -- right
    self._ruleText = self._ui["Panel/Panel_right/Panel_text/text"]
    
    -- reward
    local itemPanel = self._ui["Panel/Panel_right/Panel_item"]
    self._rewards = {0,0,0}
    self._visibleIcon = {}
	for i=1,4 do
		local con = itemPanel:getChildByName("Panel_"..i)
		self._rewards[i] = con
    end
    
    -- seasonBtn
    self._ui["Panel/Panel_right/Button"]:addClickEventListener(function()
        app:sendMsg("PvpEliteController", "openEliteMatchSeasonAwardView", self._playId)
    end)
    self._ui["Panel/Panel_right/Button/name"]:setString(L("EliteMatchTip50"))
end

function EliteMatchBookView:updateBookView(info)
    if not info then return end
    self._playId = info.playId
    self._lv = info.lv
    local shipsConf = GD:getEliteMatchShipsById(self._playId)
    if shipsConf then
        self._tltText:setString(L(info.conf.name))
        if shipsConf.elitematchDes then
            self._descText:setString(L(shipsConf.elitematchDes))
            self._descText:setVisible(true)
            self._leftBtn:setVisible(true)
        else
            self._descText:setVisible(false)
            self._leftBtn:setVisible(false)
        end
        local iconPath = string.format("%s%s.png",Res.eliteMatchPath,shipsConf.elitematchBg)
        self._icon:loadTexture(iconPath)
        self._ruleText:setString(L(shipsConf.elitematchTips))
        local rewards = CrossServerEliteMatchHelper:getLootRewardByPlayId(self._playId, self._lv, true)
        for i = 1, 4 do
            local con = self._rewards[i]
            local text = con:getChildByName("Text")
            text:setTextColor(cc.c3b(156, 255, 0))
            local name = con:getChildByName("name")
            local value = rewards[i]
            if value then               
                con:setVisible(true)
                local render = IconRender.create(con:getChildByName("icon"))
                local reward = UD:parseRewardToBagItem(value.data)
                render:setData(reward)
                render:setTipEnable(true)
                render:setNumString(reward:getAmount())
                local str = (reward:getId() == 151 or reward:getId() == 152) and L("EliteMatchTip25") or reward:getName()
                name:setString(str)
                text:setString(value.textStr)
                text:setTextColor(value.textColor)
                if reward:getId() == Constant.LightForgeGoldId then -- 光铸金币有上限
                    local contents = CrossServerEliteMatchHelper:getExtraTipsContent(reward, self._playId)
                    render:setExtraTipsContents(contents)
                else -- 赛季经验是胜利失败
                    if not value.isReward then
                        render:setNumString("-"..reward:getAmount())  
                        render:setNumberColor(cc.c3b(255, 48, 0))      
                    end
                end
                table.insert(self._visibleIcon, con)  
            else            
                con:setVisible(false)
            end
        end
        self:_autoAdjustRewardPanel()
    end
end

-- 调整奖励panel中icon的位置
function EliteMatchBookView:_autoAdjustRewardPanel()
    local visibleNum = #self._visibleIcon
    local width = self._ui["Panel/Panel_right/Panel_item"]:getContentSize2()
    if visibleNum == 1 then
        self._visibleIcon[1]:setPositionX(width*0.5)
    elseif visibleNum == 2 then
        self._visibleIcon[1]:setPositionX(width*0.32)
        self._visibleIcon[2]:setPositionX(width*0.68)
    elseif visibleNum == 3 then
        self._visibleIcon[1]:setPositionX(width*0.25)
        self._visibleIcon[2]:setPositionX(width*0.5)
        self._visibleIcon[3]:setPositionX(width*0.75)
    end
end

return EliteMatchBookView